import { post, get } from '@/utils/request'
import siteConfig from '@/config/site'
import { jsonToSearch } from '@/utils/tools'
import icons from '@/config/icons'

// 角色列表
export const getRoleListApi = (page = 1, size = siteConfig.pageSize) => {
  return get<{ total: number; roles: IRoleType.IRole[] }>(
    `/api/role/list?page=${page}&size=${size}`
  )
}

// 添加角色
export const addRoleApi = (roleData: IRoleType.IRoleForm) => {
  return post<number>(`/api/role/create`, roleData)
}

// 根据角色id返回对应的用户记录 -- 分页
export const getRoleIdToUserListApi = (
  roleid: number,
  page = 1,
  size = siteConfig.pageSize
) => {
  return get<{ total: number; users: IUserType.IUserItem[] }>(
    `/api/role/roleidToUsers/${roleid}?page=${page}&size=${size}`
  )
}

// 根据角色获取对应的菜单列表信息
export const getMenuAuthsApi = async (roleid: number) => {
  let ret = await get<any>(`/api/role/${roleid}/menuauths`)
  // 使用icons对象对菜单图标进行替换
  ret.data.menus = ret.data.menus.map((item: any) => {
    return {
      ...item,
      icon: (icons as any)[item.icon],
      children: item.children.map((child: any) => {
        return {
          ...child,
          icon: (icons as any)[child.icon]
        }
      })
    }
  })

  return ret
}
